const { resolve } = require('path')

const Koa = require('koa')
const router = require(resolve(__dirname, './router/index'))
const { koaBody } = require('koa-body')
const koaStatic = require('koa-static')
const cors = require('koa-cors')
const logger = require('koa-logger')
const moment = require('moment')

const app = new Koa()

// logger


app.use(koaBody())
  .use(logger())
  .use(async (ctx, next) => {
    const start = new Date()
    await next()
    const ms = new Date() - start
    const currentTime = moment().format('YYYY:MM:DD HH:mm:ss')
    console.log(`--> log: ${ctx.method} - ${ctx.url} - ${ms}ms 时间: ${currentTime}`)
  })
  .use(koaStatic(resolve(__dirname, '../src/upload')))
  .use(router.routes())
  .use(router.allowedMethods())
  .use(cors())




module.exports = app